package com.mfc.education.service.edu.controller.admin;


import com.mfc.education.common.base.result.R;
import com.mfc.education.service.edu.entity.Chapter;
import com.mfc.education.service.edu.entity.vo.ChapterVo;
import com.mfc.education.service.edu.service.ChapterService;
import com.mfc.education.service.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Mfc
 * @since 2021-07-25
 */
@Api(description = "章节管理")

@RestController
@RequestMapping("admin/edu/chapter")
@Slf4j
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;


    @ApiOperation("新增章节")
    @PostMapping("/save")
    public R save(
            @ApiParam(value = "章节信息对象", required = true)
            @RequestBody Chapter chapter) {

        boolean result = chapterService.save(chapter);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }

    }

    @ApiOperation("根据id查询章节")
    @GetMapping("get/{id}")
    public R getChapter(
            @ApiParam(value = "章节id", required = true)
            @PathVariable String id
    ) {
        Chapter chapter = chapterService.getById(id);

        if (chapter != null) {
            return R.ok().data("item", chapter);

        } else {
            return R.ok().message("数据不存在");
        }

    }


    @ApiOperation("根据id修改章节信息")
    @PutMapping("update")
    public R updateChapter(
            @ApiParam(value = "章节信息", required = true)
            @RequestBody Chapter chapter
    ) {
        boolean result = chapterService.updateById(chapter);
        if (result) {
            return R.ok().message("修改成功");

        } else {
            return R.error().message("数据不存在");
        }

    }


    @ApiOperation("根据id删除章节信息")
    @DeleteMapping("remove/{id}")
    public R removeChapterById(
            @ApiParam(value = "章节id", required = true)
            @PathVariable String id
    ) {
        // TODO: 删除vod视频
        //在此处调用vod中的删除视频文件的接口
        videoService.removeMediaByChapterId(id);
        boolean result = chapterService.removeChapterById(id);
        if (result) {
            return R.ok().message("删除成功");

        } else {
            return R.error().message("数据不存在");
        }

    }

    @ApiOperation("嵌套章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId) {

        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }


}

